from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time


def test():
    # Chrome选项配置
    chrome_options = Options()
    chrome_options.add_argument("--start-maximized")  # 启动时最大化窗口
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument("--remote-debugging-port=9222")  # 可选，用于调试
    driver = webdriver.Chrome(executable_path='chromedriver-win64/chromedriver.exe')

    # 指定ChromeDriver路径
    service = Service('chromedriver-win64/chromedriver.exe')  # 请确保填写实际的chromedriver路径
    driver = webdriver.Chrome(service=service, options=chrome_options)

    # 先打开一个空页面
    driver.get('https://www.yuketang.cn')
    # 添加 cookies
    cookies = [
        {'name': 'csrftoken', 'value': 'HffCqjiqS9nk4HQqavAE9zy0maaWCZTb'},
        {'name': 'sessionid', 'value': '82mqbjwojnsu1c2xrkzznldoxe3hmeyh'},
        # 可以添加其他必要的 cookie
    ]

    for cookie in cookies:
        driver.add_cookie(cookie)

    # 打开雨课堂登录页面
    driver.get('https://www.yuketang.cn/v2/web/index')

    time.sleep(8)  # 等待页面加载
    print("登录成功")

    # 登录成功后，你可以在这里添加下一步的操作
    element = driver.find_element(By.ID, 'tab-student')
    element.click()
    print("点击成功！")
    time.sleep(3)
    #寻找要刷的课程点击进入
    class_all = [
        '职场沟通与生活艺术',
        '中国话剧名作鉴赏',
        '哲思与诗意——禅诗鉴赏与创作',
        '原来姹紫嫣红开遍——昆曲艺术欣赏'

    ]
    print(len(class_all))
    for i in range(3,len(class_all)) :
        time.sleep(3)
        print(class_all[i])
        element_class_change = driver.find_elements(By.XPATH,".//div[@data-v-ba776a48 and @class='studentCol el-col el-col-24 el-col-xs-8 el-col-sm-8 el-col-md-8 el-col-lg-8 el-col-xl-6']")
        element_class_change[i].click()
        time.sleep(5)  # 等待所有页面加载
        #打开展开页面
        show_sp = driver.find_element(By.XPATH,".//span[@class='blue ml20' and @data-v-3ef83e2f and text()[normalize-space()='展开']]")
        print(show_sp.text)
        show_sp.click()
        time.sleep(5)
        #进行判断是否是视频并且不是已完成
        use_elements = driver.find_elements(By.XPATH,".//div[@data-v-721ac683 and @class='section borderb']//span[@data-v-1c75131d and (text()='未开始' or text()='进行中')]")
        print(len(use_elements))
        for j in range(len(use_elements)):
            print(use_elements)
            print(use_elements[j].text)
            use_elements[j].click()
            print("点击成功")
            time.sleep(3)
            try:
                # 点击播放视频
                click_element = driver.find_element(By.XPATH,".//xt-playbutton[@class='xt_video_player_play_btn fl']")
                click_element.click()
                time.sleep(1)
                #静音
                mute_element = driver.find_element(By.XPATH,'//*[@id="video-box"]/div/xt-wrap/xt-controls/xt-inner/xt-volumebutton/xt-icon')
                mute_element.click()
                print("开启静音")
                time.sleep(2)
                #获取时间
                all_time = driver.find_elements(By.XPATH,".//span")[10].text
                sleep_time = int(all_time.split(':')[1])*60 + int(all_time.split(':')[2])
                time.sleep(sleep_time+3)
                #看完点击返回
                back_element = driver.find_element(By.XPATH,'.//span[@data-v-4d7b8a4c and @style="line-height: 49px;"]')
                print(back_element.text)
                back_element.click()
                time.sleep(5)
            except NoSuchElementException as e:
                print("播放视频按钮未找到，进行下一次循环")
            # 返回上一个页面
            back_button = driver.find_element(By.XPATH, ".//div[@data-v-b26116f8 and @class='f14 back fl flex-disaply']")
            back_button.click()
            time.sleep(5)
            #刷新页面
            driver.refresh()
            time.sleep(5)
            #重新点击
            # 打开展开页面
            show_sp = driver.find_element(By.XPATH,
                                          ".//span[@class='blue ml20' and @data-v-3ef83e2f and text()[normalize-space()='展开']]")
            print(show_sp.text)
            show_sp.click()
            time.sleep(5)
        driver.back()
        driver.refresh()
        time.sleep(3)
    # 关闭浏览器
    driver.quit()
    #test()

def auto():
    # Chrome选项配置
    chrome_options = Options()
    chrome_options.add_argument("--start-maximized")  # 启动时最大化窗口
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument("--remote-debugging-port=9222")  # 可选，用于调试
    driver = webdriver.Chrome(executable_path='chromedriver-win64/chromedriver.exe')

    # 指定ChromeDriver路径
    service = Service('chromedriver-win64/chromedriver.exe')  # 请确保填写实际的chromedriver路径
    driver = webdriver.Chrome(service=service, options=chrome_options)

    # 先打开一个空页面
    driver.get('https://www.yuketang.cn')

    # 添加 cookies
    cookies = [
        {'name': 'csrftoken', 'value': 'Q4Rk1ktdO993RxwGMzA7CBa4UbYLJEfq'},
        {'name': 'sessionid', 'value': '9bvmw09kgasy9t0ocyv9tp7nr98q9a5f'},
        # 可以添加其他必要的 cookie
    ]

    for cookie in cookies:
        driver.add_cookie(cookie)

    # 打开雨课堂登录页面
    driver.get('https://www.yuketang.cn/v2/web/index')

    time.sleep(5)  # 等待页面加载
    print("登录成功")

    # 登录成功后，你可以在这里添加下一步的操作
    element = driver.find_element(By.ID, 'tab-student')
    element.click()
    print("点击成功！")
    time.sleep(2)

    # 寻找要刷的课程点击进入
    class_all = [
        '职场沟通与生活艺术',
        '中国话剧名作鉴赏',
        '哲思与诗意——禅诗鉴赏与创作',
        '原来姹紫嫣红开遍——昆曲艺术欣赏'

    ]
    print(len(class_all))
    for i in range(3,len(class_all)) :
        print(class_all[3])



    # 关闭浏览器
    driver.quit()

if __name__ == "__main__":
    test()
